#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "elog.h"

#define log_a(...) elog_a("main.test.a", __VA_ARGS__)
#define log_e(...) elog_e("main.test.e", __VA_ARGS__)
#define log_w(...) elog_w("main.test.w", __VA_ARGS__)
#define log_i(...) elog_i("main.test.i", __VA_ARGS__)
#define log_d(...) elog_d("main.test.d", __VA_ARGS__)
#define log_v(...) elog_v("main.test.v", __VA_ARGS__)

static void *test_elog(void);

int main(void) {
    //setbuf(stdout, NULL);
    /* initialize EasyLogger */
    elog_init("elogtest");
    /* set EasyLogger log format */
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_ALL);


    /* dynamic set enable or disable for output logs (true or false) */
//    elog_set_output_enabled(false);
    /* dynamic set output logs's level (from ELOG_LVL_ASSERT to ELOG_LVL_VERBOSE) */
//    elog_set_filter_lvl(ELOG_LVL_WARN);

    /* test logger output */
//    thread_test();
    test_elog();
    elog_release();
    return EXIT_SUCCESS;
}

/**
 * EasyLogger demo
 */
void *test_elog(void) {
//	thread_size();
    do{
        /* test log output for all level */
        log_a("Hello EasyLogger!");
        log_e("Hello EasyLogger!");
        log_w("Hello EasyLogger!");
        log_i("Hello EasyLogger!");
        log_d("Hello EasyLogger!");
        log_v("Hello EasyLogger!");
        sleep(5);
    }while(0);
}
void thread_test(){
	pthread_attr_t pattr;
	struct sched_param param;

	param.sched_priority = 23;
	pthread_attr_init(&pattr);
	pthread_attr_setschedpolicy(&pattr, SCHED_RR);
	pthread_attr_setschedparam(&pattr, &param);
	pthread_attr_setinheritsched(&pattr, PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED);
	pthread_attr_setstacksize(&pattr, 20480);
	pthread_create(NULL, &pattr, &test_elog, NULL);
}
void thread_size()
{
	pthread_attr_t pattr;
	size_t size = 0;
	log_d("default size:%d", size);
	pthread_attr_getstacksize(&pattr, &size);
	log_d("current thread [%d] stack size:%d", gettid(), size);
}
